﻿using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Text;
using System.Transactions;

namespace _1Dot9MasterTransactions
{
    internal class TransactionInfo
    {
        // Fields
        private string _ApplicationName;
        private string _ClassName;
        private bool _IsManualComplete;
        private IsolationLevel _IsolationLevel;
        private object _RawData;
        private int _Timeout;
        private bool _TrackingEnabled;
        private TransactionOption _TransactionOption;

        // Methods
        public TransactionInfo()
        {
            this._ApplicationName = null;
            this._ClassName = null;
            this._TrackingEnabled = true;
            this._IsManualComplete = false;
            this._TransactionOption = TransactionOption.None;
            this._IsolationLevel = IsolationLevel.Unspecified;
            this._Timeout = 0;
        }

        public TransactionInfo(TransactionAttribute attribute)
            : this()
        {
            this._TransactionOption = attribute.TransactionOption;
            this._IsolationLevel = attribute.IsolationLevel;
            this._Timeout = attribute.Timeout;
        }

        // Properties
        public string ApplicationName
        {
            get
            {
                return this._ApplicationName;
            }
            set
            {
                this._ApplicationName = value;
            }
        }

        public string ClassName
        {
            get
            {
                return this._ClassName;
            }
            set
            {
                this._ClassName = value;
            }
        }

        public TransactionIsolationLevel ComIsolationLevel
        {
            get
            {
                switch (this._IsolationLevel)
                {
                    case IsolationLevel.Serializable:
                        return TransactionIsolationLevel.Serializable;

                    case IsolationLevel.RepeatableRead:
                        return TransactionIsolationLevel.RepeatableRead;

                    case IsolationLevel.ReadCommitted:
                        return TransactionIsolationLevel.ReadCommitted;

                    case IsolationLevel.ReadUncommitted:
                        return TransactionIsolationLevel.ReadUncommitted;

                    case IsolationLevel.Snapshot:
                        throw new ArgumentException("COM+ doesnot support 'Snapshot' transaction isolation level");

                    case IsolationLevel.Chaos:
                        throw new ArgumentException("COM+ doesnot support 'Chaos' transaction isolation level");

                    case IsolationLevel.Unspecified:
                        return TransactionIsolationLevel.Any;
                }
                throw new ArgumentException("Unknown IsolationLevel value(" + ((int)this._TransactionOption).ToString() + ") specified");
            }
        }

        public System.EnterpriseServices.TransactionOption ComTransactionOption
        {
            get
            {
                switch (this._TransactionOption)
                {
                    case TransactionOption.None:
                        return System.EnterpriseServices.TransactionOption.Supported;

                    case TransactionOption.Required:
                        return System.EnterpriseServices.TransactionOption.Required;

                    case TransactionOption.RequiresNew:
                        return System.EnterpriseServices.TransactionOption.RequiresNew;

                    case TransactionOption.Suppress:
                        return System.EnterpriseServices.TransactionOption.NotSupported;
                }

                throw new ArgumentException("Unknown TransactionOption value(" + ((int)this._TransactionOption).ToString() + ") specified");
            }
        }

        public bool IsManualComplete
        {
            get
            {
                return this._IsManualComplete;
            }
            set
            {
                this._IsManualComplete = value;
            }
        }

        internal object RawData
        {
            get
            {
                return this._RawData;
            }
            set
            {
                this._RawData = value;
            }
        }

        public int TimeOut
        {
            get
            {
                return this._Timeout;
            }
        }

        public bool TrackingEnabled
        {
            get
            {
                return this._TrackingEnabled;
            }
            set
            {
                this._TrackingEnabled = value;
            }
        }

        public TransactionOption TransactionOption
        {
            get
            {
                return this._TransactionOption;
            }
            set
            {
                this._TransactionOption = value;
            }
        }

        public System.Transactions.IsolationLevel TxIsolationLevel
        {
            get
            {
                return (System.Transactions.IsolationLevel)this._IsolationLevel;
            }
        }

        public TransactionScopeOption TxTransactionOption
        {
            get
            {
                return (TransactionScopeOption)this._TransactionOption;
            }
        }
    }
}
